package com.demo.controller;

import com.demo.entity.ConfigDTO;
import com.demo.entity.LogDTO;
import com.demo.entity.TypicalModel;
import com.demo.service.DemoService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guns21.data.domain.result.MessageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

@Controller
public class DemoController {

    @Autowired
    private DemoService demoService;
    @Autowired
    private ConfigDTO configDTO;
    /**
     * 资源属性配置的基本使用方法
     * 2018-04-04
     * @return
     */
    @RequestMapping("/getConfig")
    @ResponseBody
    public Object getConfig(){
        //这里需要将configDTO重新copy属性到新的对象中，因为configDTO EnhancerBySpringCGLIB ，不能直接作为json序列化返回
        ConfigDTO config = new ConfigDTO();
        BeanUtils.copyProperties(configDTO,config);
        return config;
    }

    /**
     * jackson的基本使用方法
     * 2018-04-04
     * @return
     */
    @RequestMapping("/getDemoModel")
    @ResponseBody
    public Object getTypicalModel(){
        TypicalModel model = new TypicalModel();
        model.setAge(20);
        model.setName("wahaha");
        model.setNames("names");
        model.setStartDate(new Date());
        model.setValidateTime(new Timestamp(new Date().getTime()));
        return model;
    }

    /**
     * lombok 使用
     * 2018-04-04
     * @return
     */
    @RequestMapping("/getObject")
    @ResponseBody
    public Object getObject(){
//        LogDTO.LogDTOBuilder builder = LogDTO.builder().note("this is a simple demo");
//        return builder.build().getNote();
//        return "this is a simple demo";

//        return readObject("List");
        return readNode();
    }

    /**
     * ObjectMapper read object
     * @param str
     * @return
     */
    private Object readObject(String str){
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = "{\"note\":\"this is object\"}";
        switch (str){
            case "String":
                String returnString = "";
                LogDTO logDTOValue;
                try {
                    logDTOValue = objectMapper.readValue(jsonStr,LogDTO.class);
                    returnString = objectMapper.writeValueAsString(logDTOValue);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return returnString;
            case "Map":
                Map map = new HashMap();
                try {
                    map = objectMapper.readValue(jsonStr,Map.class);
                } catch (IOException e) {
                    e.printStackTrace();
                    map.put("key",e.getMessage());
                }
                return map;
            case "List":
                List<String> list = new ArrayList<String>();
                try {
                      jsonStr = "["+jsonStr+"]";
                      list = objectMapper.readValue(jsonStr,List.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    list.add(e.getMessage());
                }
                return list;
        }
        return null;
    }

    private Object readNode(){
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonTree = "{\"name\":\"diandian2u\",\"properties\":{\"hobby\":\"climb\",\"bloodType\":\"B\"}}";
        String content = "";
        try {
            JsonNode root = objectMapper.readTree(jsonTree);
            String name = root.get("name").asText();
            Iterator<JsonNode> nodes = root.get("properties").elements();
            while(nodes.hasNext()){
                content += nodes.next().textValue();
            }
        } catch (IOException e) {
            e.printStackTrace();
            content=e.getMessage();
        }
        return content;
    }

    /**
     * 测试mybatis的insert
     * @param logDTO
     * @param bindingResult
     * @return
     */
    @RequestMapping("/insertLog")
    @ResponseBody
    public Object insertLog(@Validated LogDTO logDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return MessageResult.fail("当前传参有问题");
        }
        demoService.insertLog(logDTO);
        return "ok";
    }

    /**
     * 测试查询
     * @return
     */
    @RequestMapping("/getLog")
    @ResponseBody
    public List getLog(){
        return demoService.getLog();
    }

    /**
     * 使用pagehelper
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public List getList(int pageNum,int pageSize){
        List list = demoService.getList(pageNum,pageSize);
        return list;
    }
}
